//
//  SearchTableViewController.m
//  mpoi
//
//  Created by thomasho on 15/12/3.
//  Copyright © 2015年 thomasho. All rights reserved.
//

#import "SearchTableViewController.h"

@interface SearchTableViewController ()
@property (nonatomic,strong) NSArray     *searchArray;
@end

@implementation SearchTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchArray = self.dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.searchArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list" forIndexPath:indexPath];
    NSDictionary *dict = self.searchArray[indexPath.row];
    //to do
    cell.textLabel.text = [dict valueForJSONStrKey:@"name"];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.searchArray[indexPath.row];
    //to do
    [self.sender performSelector:@selector(searchBack:) withObject:dict];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchKey:searchText];
}
- (void)searchKey:(NSString*)key {
    if (key.length==0) {
        self.searchArray = self.dataArray;
    }else{
        NSMutableArray *marray = [NSMutableArray array];
        for (NSDictionary *dict in self.dataArray) {
            if ([[dict valueForJSONStrKey:@"code"] rangeOfString:key].length==key.length||[[dict valueForJSONStrKey:@"name"] rangeOfString:key].length==key.length) {
                [marray addObject:dict];
            }
        }
        self.searchArray = marray;
    }
    [self.tableView reloadData];
}
@end
